package cn.dglydrpy.study.algorithm.od;

import java.util.*;

/**
 * @author Yyy
 * @date 2024/11/7 9:24
 * @description 给定一组确诊人员编号，找出需要进行核酸检测的人数。
 输入：
 5
 1,2
 1,1,0,1,1
 1,1,0,0,0
 0,0,1,0,1
 1,0,0,1,0
 0,0,1,0,1
 输出：
 3
 */
public class Od0015_HSJC {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()){
            int n = Integer.valueOf(scanner.nextLine());
            String[] s = scanner.nextLine().split(",");
            int[] sure = new int[s.length];
            for(int i=0;i<sure.length;i++){
                sure[i] = Integer.valueOf(s[i]);
            }
            int[][] record = new int[n][n];
            for(int i=0;i<record.length;i++){
                String[] s1 = scanner.nextLine().split(",");
                for(int j=0;j<record[0].length;j++){
                    record[i][j] = Integer.valueOf(s1[j]);
                }
            }
            
            int ans = myAnswer(n,record,sure);
            System.out.println(ans);
            int answer1 = answer1(n,record,sure);
            System.out.println(answer1);
            
        }
    }

    private static int myAnswer(int n, int[][] record, int[] sure) {
        if(sure.length == 0 || record.length == 0 || record[0].length == 0){
            return 0;
        }

        Set<Integer> sureSet = new HashSet<Integer>();
        LinkedList<Integer> queue = new LinkedList<>();
        for (int s : sure){
            sureSet.add(s);
            queue.add(s);
        }

        Set<Integer> ans = new HashSet<>();
        
        while(!queue.isEmpty()){
            Integer i = queue.poll();
            for(int j=0;j<n;j++){
                if(record[i][j] == 1){
                    if(!ans.contains(j) && !sureSet.contains(j)){
                        ans.add(j);
                        queue.add(j);
                    }
                }
            }
        }
        

//        ans.forEach(System.out::println);

        return ans.size();
    }


    public static int answer1(int n, int[][] record, int[] sure) {

        Set<Integer> tempSet = new HashSet();
        for (int i = 0; i < sure.length; i++) {
            int aim = sure[i];
            for (int j = 0; j < n; j++) {
                if (record[aim][j] == 1 && aim!= j) {
                    tempSet.add(j);
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j) {
                    continue;
                }
                if (record[i][j] == 1 && tempSet.contains(i)) {
                    tempSet.add(j);
                }
                if (record[j][i] == 1 && tempSet.contains(j)) {
                    tempSet.add(i);
                }
            }
        }
        int size = tempSet.size();
        for (int i : sure) {
            if (tempSet.contains(i)) {
                size--;
            }
        }
        return size;
    }
}
